<?php
// @formatter:off
/**
 * @file c.edit.COLLECTOR-CMOVE.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-04 03:02:53 +0000 (Tue, 04 Jun 2013) $
 *
 * $Id: c.edit.COLLECTOR-CMOVE.php 68 2013-06-04 03:02:53Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/extensions/DirectoryOrganizer/controlers/c.edit.COLLECTOR-CMOVE.php $
 */
// @formatter:on

global $wcDirectoriesHolder;
global $wcCollectionsHolder;

switch($_POST["EDITACTION"]) {
	case "SET":
		if(isset($_POST["ACTION_DIRECTORY"]) && isset($_POST["ACTION_COLLECTION"])) {
			$diretoryName = explode(",", $_POST["ACTION_DIRECTORY"]);
			$collectionPath = explode(",", $_POST["ACTION_COLLECTION"]);

			if(count($diretoryName) > 0 && count($collectionPath) > 0) {
				$diretoryName = array_shift($diretoryName);
				$currentDirectory = $wcRuleAction->directory();
				$directory = $wcDirectoriesHolder->itemByName($diretoryName, $wcGroup);
				if(!$directory || !$directory->ok() && $directory->id == $currentDirectory->id) {
					$directory = null;
				}

				$collectionPath = array_shift($collectionPath);
				$currentCollection = $wcRuleAction->collection();
				$collection = $wcCollectionsHolder->itemByPath($collectionPath, $wcGroup);
				if(!$collection || !$collection->ok() && $collection->id == $currentCollection->id) {
					$collection = null;
				}
				//
				// If I have everything I need, I try it
				if($directory && $collection) {
					//
					// Checking if it's an update or an insert.
					if($currentCollection && $currentDirectory) {
						$stmt = null;
						$stmtId = __CLASS__."::".__FUNCTION__."[COLLECTOR-INSERT]";
						if($db->has($stmtId)) {
							$stmt = $db->get($stmtId);
						} else {
							$query = "update  {$dbprefix}acts_dor\n";
							$query .= "set     act_collection = :collection,\n";
							$query .= "        act_directory  = :directory\n";
							$query .= "where   act_rule   = :rule\n";
							$query .= " and    act_group  = :group\n";
							$query .= " and    act_action = :action\n";

							$stmt = $db->prepare($stmtId, $query);
						}
						$params = array(
							":rule" => $wcRule->id(),
							":group" => $wcGroup->id,
							":action" => $wcRuleAction->id(),
							":collection" => $collection->id,
							":directory" => $directory->id,
						);
						$stmt->execute($params);
					} else {
						$stmt = null;
						$stmtId = __CLASS__."::".__FUNCTION__."[COLLECTOR-INSERT]";
						if($db->has($stmtId)) {
							$stmt = $db->get($stmtId);
						} else {
							$query = "insert\n";
							$query .= "        into {$dbprefix}acts_dor (\n";
							$query .= "                act_rule, act_group, act_action, act_collection, act_directory, act_user)\n";
							$query .= "        values (:rule, :group, :action, :collection, :directory, :user)\n";

							$stmt = $db->prepare($stmtId, $query);
						}
						$params = array(
							":rule" => $wcRule->id(),
							":group" => $wcGroup->id,
							":action" => $wcRuleAction->id(),
							":collection" => $collection->id,
							":directory" => $directory->id,
							":user" => $wcUser->id
						);
						$stmt->execute($params);
					}
				}

				$wcRuleAction->reload();
			}
		}

		break;
	default:
		break;
}
?>